/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air.servlet;

import air.eb.Flightinstance;
import air.sb.AirRemote;
import air.sb.LuftRemote;
import air.sb.SASRemote;
import java.io.*;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 *
 * @author JonasF
 * Handles the call from HttpRequestHandler and handels the FindTrip request by calling
 * the session AirBean and redirects to the right JSPX-page
 */
public class FindTrip extends HttpServlet {

    @EJB
    private AirRemote airBean;
    @EJB
    private SASRemote sASBean;
    @EJB
    private LuftRemote luftBean;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
//        request.getRequestDispatcher("index.jspx").forward(request, response);
        try {
            //collects all data needed for the AirBean to collect the
            //right data from the data base
            String sDate = request.getParameter("date");
            String dep = request.getParameter("departure");
            String dest = request.getParameter("destination");
            int nrPassenger = Integer.parseInt(request.getParameter("nrP"));

            airBean.findArrivalFlights(dest, nrPassenger);
            luftBean.findArrivalFlights(dest, nrPassenger);
            sASBean.findArrivalFlights(dest, nrPassenger);

            airBean.findDepartureFlights(dep, nrPassenger);
            luftBean.findDepartureFlights(dep, nrPassenger);
            sASBean.findDepartureFlights(dep, nrPassenger);

            airBean.findRoutes(sDate, dep, dest, nrPassenger);
            luftBean.findRoutes(sDate, dep, dest, nrPassenger);
            sASBean.findRoutes(sDate, dep, dest, nrPassenger);


            ArrayList<Flightinstance> arrFlights = new ArrayList<Flightinstance>();
            arrFlights.addAll(airBean.getArrivalFlights());
            arrFlights.addAll(luftBean.getArrivalFlights());
            arrFlights.addAll(sASBean.getArrivalFlights());
            ArrayList<Flightinstance> depFlights = new ArrayList<Flightinstance>();
            depFlights.addAll(airBean.getDepartureFlights());
            depFlights.addAll(luftBean.getDepartureFlights());
            depFlights.addAll(sASBean.getDepartureFlights());


            possibleRoutes(dep, dest, depFlights, arrFlights);
//
            //            //supposes sDate is on format yyyy-mm-dd
            //            Date date = new Date((Integer.parseInt(sDate.substring(0, 4)) - 1900), (Integer.parseInt(sDate.substring(5, 7)) - 1), Integer.parseInt(sDate.substring(8, 10)));
            //            Date d = new Date();
            //            if (date.compareTo(d) > 0) {
            //                AirRemote AB = lookupAirBean();
            //                AB.findFlights(sDate, dep, dest, nrPassenger);
            //            }
        } finally {
            //redirects to the right JSPX
            response.sendRedirect("ChooseTrip.jspx");
            out.close();
        }
    }

    public void possibleRoutes(String d, String a, ArrayList<Flightinstance> depList, ArrayList<Flightinstance> arrList) {
        System.out.println("Size: " + depList.size());
        for (Flightinstance fi : depList) {
            System.out.println("Size: " + depList.size() + "depAirport: " + fi.getDepAirport() + "flight: " + fi.getNr());
        }
        for (Flightinstance fi : arrList) {
            System.out.println(fi.getArrAirport() + " arr " + fi.getNr());
        }
        for (int i = 0; i < depList.size(); i++) {
            for (int j = 0; j < arrList.size(); j++) {
                if (depList.get(i).getNr().equals(arrList.get(j).getNr())){// && depList.get(i).getDepAirport().equals(d) && arrList.get(j).getArrAirport().equals(a)) {
                    System.out.println("Direktflyg nr: " + depList.get(i).getNr());
                }
                if (depList.get(i).getDepAirport().equals(d) && depList.get(j).getDepAirport().equals(depList.get(i).getArrAirport()) && (arrList.get(j).getArrAirport().equals(a))) {
                    System.out.println("1 stop flight: " + depList.get(i).getNr() + " - " + depList.get(j).getNr());
                }
            }

//            for (int k = 0; k < arrList.size(); k++) {
//                for (int l = 0; l < arrList.size(); l++) {
//                    if ((depList.get(i).getDep().equals(d)) && (depList.get(k).getDep().equals(depList.get(i).getArr())) && (depList.get(l).getDep().equals(depList.get(k).getArr())) && (arrList.get(l).getArr().equals(a))) {
//                        System.out.println("2 stop flight: " + depList.get(i).getName() + " - " + depList.get(k).getName() + " - " + arrList.get(l).getName());
//                    }
//                }
//            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
